﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Media;

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ControlExplorer.View
{
    /// <summary>
    /// The built-in ApplicationBar in WindowsPhone is not a native Silverlight control,
    /// which forbids you from using Binding and other basic functionality.
    /// 
    /// This class expose a few properties in the IApplicationBar interface as DependencyProperties
    /// so they can be used in Binding scenarios. When placed in a PhoneApplicationPage, it will find 
    /// the ApplicationBar and forward its properties to the ApplicationBar.
    /// </summary>
    public class ApplicationBarBindingProvider
    : FrameworkElement
    {
        public ApplicationBarBindingProvider()
        {
            ViewModel.Instance.PropertyChanged += Instance_PropertyChanged;
            Loaded += ApplicationBarBindingUpdater_Loaded;
        }

        void ApplicationBarBindingUpdater_Loaded(object sender, RoutedEventArgs e)
        {
            var page = C1.Phone.Extensions.FindParent<PhoneApplicationPage>(this);
            this.ApplicationBar = page.ApplicationBar;
        }

        void Instance_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "InheritPhoneTheme")
            {
                UpdateApplicationBar();
            }
        }

        void UpdateApplicationBar()
        {
            if (this.ApplicationBar != null)
            {
                this.ApplicationBar.ForegroundColor = ForegroundColor;
                this.ApplicationBar.BackgroundColor = BackgroundColor;
            }
        }

        #region replicated properties

        public Color ForegroundColor
        {
            get { return (Color)GetValue(ForegroundColorProperty); }
            set { SetValue(ForegroundColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ForegroundColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ForegroundColorProperty =
            DependencyProperty.Register("ForegroundColor", typeof(Color), typeof(ApplicationBarBindingProvider), new PropertyMetadata(Colors.White, OnPropertyChanged));

        public Color BackgroundColor
        {
            get { return (Color)GetValue(BackgroundColorProperty); }
            set { SetValue(BackgroundColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BackgroundColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BackgroundColorProperty =
            DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(ApplicationBarBindingProvider), new PropertyMetadata(Colors.Black, OnPropertyChanged));

        #endregion

        public IApplicationBar ApplicationBar
        {
            get { return (IApplicationBar)GetValue(ApplicationBarProperty); }
            set { SetValue(ApplicationBarProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ApplicationBar.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ApplicationBarProperty =
            DependencyProperty.Register("ApplicationBar", typeof(IApplicationBar), typeof(ApplicationBarBindingProvider), new PropertyMetadata(null, OnPropertyChanged));

        static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            ((ApplicationBarBindingProvider)sender).UpdateApplicationBar();
        }
    }
}
